Game = Class.extend({

        init: function( context,
        				id,	
                        gameField, 
                        backgroundImage, 
                        gunImage, 
                        images, 
                        audios, 
                        gunAudio, 
                        munitions,
                        playerMoney,
                        playerPoint,
                        counterDeadEnemy
                        ) {
		this.context = context;
		this.id = id;
		
        this.gameField = gameField;

        this.backgroundImage = backgroundImage;
        this.image = images;
        this.audios = audios;
        
        this.gunImage = gunImage;
        this.gunAudio = gunAudio;
        this.munitions = munitions;

        this.playerMoney = playerMoney;
        this.playerPoint = playerPoint;
        this.counterDeadEnemy = counterDeadEnemy;
        
        this.bonusMoney = 0;
        this.bonusPoints = 0;
        
        this.playerWasKilled = false;
        
        this.start();
                
	},
        
	initObjects: function() {
     
        this.gunAnimation = new Gun(
                this.context,
                200,
                200,
                this.gunImage,
                this.munitions
        );
	},
	
	initEnemy: function() {
		var self = this;
    	
		//Level 1
        if(this.backgroundImage == this.image[10]){
        	self.enemyLevel = new Level1(self.context, self.image, self.audios[5] );
	        self.enemyLevel.initEnemyLevel();

        };
        
      //Level 2
        if(this.backgroundImage == this.image[11]){
        	self.enemyLevel = new Level2(self.context, self.image, self.audios[5]);
        	self.enemyLevel.initEnemyLevel();

        };
      
      //Level 3
        if(this.backgroundImage == this.image[12]){
        	self.enemyLevel = new Level3(self.context, self.image, self.audios[5]);
	        self.enemyLevel.initEnemyLevel();
        };
	        
	},
        checkKilledPlayer : function (shootingInterval) {
            var self = this;
            if(this.playerWasKilled){
                    clearInterval(shootingInterval)
                    setTimeout(function(){
                        var gameField = document.getElementById(self.gameField);
                        gameField.style.backgroundImage = "url("+self.image[17].src+")";
                        gameField.style.cursor = "auto";
                        self.gunAnimation.clear(0,0,1024,720);

                        var end = new EndGame(
                            self.context,
                            self.id,
                            self.playerPoint
                                    );

                    },1000);
            }
        },
        
        
    start: function() {
            var self=this;
            var x;
            var y;
            var flag = false;
            var shootingInterval;
            
         // ********************* init object ************************
	        this.initObjects();
	        
	        
	      //********* munition ***********
	        this.gunMunitions = this.gunAnimation.munitions;
	        
	        this.gunAnimation.clear(0,0,1024,720);
	        var self = this;
	        
	        var gameField = document.getElementById(this.gameField);
	        
	        gameField.style.backgroundImage = "url("+this.backgroundImage.src+")";
	        
	        
	     // ********************* init  enemy ************************
	       
            this.initEnemy()
	        
	     // ************* gun **********
  
            //draw points at the start of the game
            self.context.fillStyle = "white";                        
            self.context.shadowColor = '#000';
            self.context.font ="20px Impact, fantasy";
            self.gunAnimation.clear(0,0,250,250);
            self.context.fillText("Points: "+ self.playerPoint,20,100);
            self.context.fillStyle = "gold";  
            self.context.fillText("Money: "+ self.playerMoney,20,120);
            
            
            //mousemove for mooving gun
            document.addEventListener('mousemove', function (event){
                if(self.counterDeadEnemy > 0 && self.playerWasKilled === false){
                    x = event.pageX-gameField.offsetLeft;
                    y = event.offsetY;
                    
                }

            });
            
	    //shoot
            var firstClick;
            var reloadFlag = false;
            var count=0;
            var spriteCount = 200;
            var arrayWithImages = self.gunAnimation.shoot(0);
            var arrayWithReloadImages = self.gunAnimation.reload(0);
            var checkEnd;
            var end = false;
            
            shootingInterval=setInterval(function(){
                
                document.onmouseup = function(){
                    flag = false;
                    count=0;
                },
                document.onmousedown = function(){
                    flag = true;
                    count=0;

                },
                document.onkeydown = function(e){
                    if(e.keyCode === 32){
                        reloadFlag = true;
                    }

                }
                
                
                if(flag && reloadFlag===false){
                
                    if(self.gunAnimation.munitions > 0 ){
                        
                        if(end === true){
                            count = 0;
                            end=false;
                        }
                        if(end === false){
                            if(count===0){
                                self.gunAnimation.munitions--;
                                self.gunAudio.load();
                                self.gunAudio.play();
                            }
                            count++;
                            self.gunAnimation.fastShoot(count,
                                                        arrayWithImages,
                                                        x,
                                                        self.gunAnimation);
                            
                            
                            if(count===4){
                                end=true;
                                count = 0;
                                self.gunAnimation.clear(0,503,1024,170);
                                self.gunAnimation.draw(0,0,x);
                                
                            }
                        }
                        //kill Enemy
                        
                        
                        if(firstClick){
                            self.counterDeadEnemy--;
                        }
                        self.enemyLevel.checkedPosition([x, y],self.counterDeadEnemy);
                        firstClick = self.enemyLevel.returnDeadEnemy();
                        console.log('counterDeadEnemy'+self.counterDeadEnemy);
                        if(firstClick){
                            self.bonusMoney += 75;
                            self.bonusPoints += 100;

                            self.playerMoney += self.bonusMoney;
                            self.playerPoint += self.bonusPoints;
                        }else{
                            self.bonusMoney = 75;
                            self.bonusPoints = 100;
                        }
                        
                        
                    }else{
                            self.gunAnimation.clear(0,503,1024,170);
                            self.gunAnimation.draw(0,0,x);
                        }
                }else if(reloadFlag){
                    
                    if(count === 0 && spriteCount === 200){
                        self.audios[3].load();
                        self.audios[3].play();
                    }
                    
                    if(count%4===0){
                        self.gunAnimation.clear(0,503,1024,170);
                        self.gunAnimation.draw(arrayWithReloadImages[count/2],spriteCount,x);
                    }
                    count++;
                    if(count === 8 && spriteCount === 200){
                        spriteCount = 400;
                        count = 0;

                    }else if(count === 8 && spriteCount === 400){
                        self.gunAnimation.munitions=self.gunMunitions;
                        count=0;
                        spriteCount = 200;
                        reloadFlag = false;
                        self.gunAnimation.clear(0,503,1024,170);
                        self.gunAnimation.draw(0,0,x);

                    }
                }else{
                    self.gunAnimation.clear(0,503,1024,170);
                    self.gunAnimation.draw(0,0,x);
                    
                }
                self.playerWasKilled = self.enemyLevel.returnDeadPlayer();
                self.checkKilledPlayer(shootingInterval);

                
                //***************enemy
                
                
                
                //draw points 
                
                self.context.save();
                
                self.context.fillStyle = "white";                        
                self.context.shadowColor = '#000';
                self.context.font ="20px Impact, fantasy";
                self.gunAnimation.clear(0,0,250,250);
                self.context.fillText("Points: "+ self.playerPoint,20,100);
                self.context.fillStyle = "gold";  
                self.context.fillText("Money: "+ self.playerMoney,20,120);
                self.context.fillStyle = "gold";  
                self.context.fillText("Munition: "+ self.gunAnimation.munitions,20,140);
                self.context.restore();
                
                //pick next level or finish the game
                if(self.counterDeadEnemy === 1){
                    clearInterval(shootingInterval);
                    
                    if(self.backgroundImage === self.image[12]){
                        setTimeout(function(){
                            var gameField = document.getElementById(self.gameField);
                            gameField.style.backgroundImage = "url("+self.image[17].src+")";
                            gameField.style.cursor = "auto";
                            self.gunAnimation.clear(0,0,1024,720);

                            var end = new EndGame(
                                self.context,
                                self.id,
                                self.playerPoint
                                        );
                        },1000);
                        
                        }else{
                            setTimeout(function(){
                                var gameField = document.getElementById(self.gameField);
                                gameField.style.backgroundImage = "url("+self.image[17].src+")";
                                gameField.style.cursor = "url('../images/game/cursor.png'), pointer";
                                self.gunAnimation.clear(0,0,1024,720);

                                clearInterval(shootingInterval);

                                var pickLevel = new LevelGunPick(
                                    self.context,
                                    self.id,
                                    self.image,
                                    self.audios,
                                    self.playerMoney,
                                    self.playerPoint
                                            );
                            },1000);

                        }
                    
                }
            },50)
	}, // start method


            
});